﻿<Window x:Class="CjcPhotoPrint.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:local="clr-namespace:CjcPhotoPrint"
    Title="CJC Photo Print" Width="700" Height="500"
	Name="photoPrint" AllowDrop="True" Drop="Window_Drop" DragOver="Window_DragOver">

	<Window.CommandBindings>
		<CommandBinding Command="ApplicationCommands.Open" Executed="Open_Executed"/>
		<CommandBinding Command="ApplicationCommands.SaveAs" Executed="SaveAs_Executed"/>
		<CommandBinding Command="ApplicationCommands.Print" Executed="Print_Executed"/>
		<CommandBinding Command="ApplicationCommands.Close" Executed="Close_Executed"/>
		<CommandBinding Command="local:MainWindow.About" Executed="About_Executed"/>
	</Window.CommandBindings>

	<Grid>

		<Grid.Resources>

			<local:NonNullConverter x:Key="nonNullConverter"/>

			<Style x:Key="radioButtonList" TargetType="{x:Type ListBox}">
				<Setter Property="BorderBrush" Value="{x:Null}" />
				<Setter Property="BorderThickness" Value="0" />
				<Setter Property="ItemContainerStyle">
					<Setter.Value>
						<Style TargetType="{x:Type ListBoxItem}" >
							<Setter Property="Margin" Value="2" />
							<Setter Property="Template">
								<Setter.Value>
									<ControlTemplate TargetType="{x:Type ListBoxItem}">
										<Border Background="Transparent">
											<RadioButton Focusable="False" IsHitTestVisible="False" IsChecked="{TemplateBinding IsSelected}">
												<ContentPresenter />
											</RadioButton>
										</Border>
									</ControlTemplate>
								</Setter.Value>
							</Setter>
						</Style>
					</Setter.Value>
				</Setter>
			</Style>

			<DataTemplate DataType="{x:Type local:IdentityContext}">
				<Border BorderThickness="0,0,0,1" BorderBrush="#E0E0E0" Padding="5" SnapsToDevicePixels="True">
					<TextBlock Text="{Binding Name}" FontStyle="Italic"/>
				</Border>
			</DataTemplate>

			<DataTemplate DataType="{x:Type local:Rotate}">
			</DataTemplate>

			<DataTemplate DataType="{x:Type local:Crop}">
				<DockPanel>
					<CheckBox DockPanel.Dock="Right" VerticalAlignment="Top" IsChecked="{Binding IsLandscape, Mode=TwoWay}">
						<CheckBox.Template>
							<ControlTemplate TargetType="CheckBox">
								<Canvas Width="20" Height="30">
									<Canvas.LayoutTransform>
										<RotateTransform x:Name="angle" Angle="0" CenterX="10" CenterY="15"/>
									</Canvas.LayoutTransform>
									<Polygon Stroke="#505050" Points="0,0 10,0 10,10 20,10 20,30 0,30" Fill="WhiteSmoke"/>
									<Polygon Stroke="#505050" Points="10,0 20,10, 10,10" Fill="White"/>
								</Canvas>
								<ControlTemplate.Triggers>
									<Trigger Property="IsChecked" Value="True">
										<Trigger.EnterActions>
											<BeginStoryboard>
												<Storyboard TargetName="angle" TargetProperty="Angle">
													<DoubleAnimation To="90" Duration="0:0:0.5"/>
												</Storyboard>
											</BeginStoryboard>
										</Trigger.EnterActions>
										<Trigger.ExitActions>
											<BeginStoryboard>
												<Storyboard TargetName="angle" TargetProperty="Angle">
													<DoubleAnimation To="0" Duration="0:0:0.5"/>
												</Storyboard>
											</BeginStoryboard>
										</Trigger.ExitActions>
									</Trigger>
								</ControlTemplate.Triggers>
							</ControlTemplate>
						</CheckBox.Template>
					</CheckBox>
					<ListBox IsSynchronizedWithCurrentItem="True" Background="Transparent"
							 ItemsSource="{Binding Constraints}" DisplayMemberPath="Name"
							 Style="{StaticResource radioButtonList}" HorizontalAlignment="Left"/>
				</DockPanel>
			</DataTemplate>

			<DataTemplate DataType="{x:Type local:Straighten}">
			</DataTemplate>

			<DataTemplate DataType="{x:Type local:TransformationContext}">
				<Border BorderThickness="0,0,0,1" BorderBrush="#E0E0E0" Padding="5" SnapsToDevicePixels="True">
					<DockPanel>
						<StackPanel DockPanel.Dock="Left" Orientation="Horizontal" VerticalAlignment="Top">
							<CheckBox VerticalAlignment="Center" IsChecked="{Binding Enabled, Mode=TwoWay}" Margin="0,0,5,0"/>
							<Button Name="delete" VerticalAlignment="Center" Content="X" FontSize="8" Margin="0,0,5,0" Click="delete_Click"/>
						</StackPanel>
						<StackPanel>
							<TextBlock VerticalAlignment="Center" Text="{Binding Name}"/>
							<ContentControl Content="{Binding Transformation}"/>
						</StackPanel>
					</DockPanel>
					<!--<GridViewRowPresenter Margin="0,2,0,2" Content="{Binding}" Columns="{StaticResource transformationColumns}"/>-->
				</Border>
			</DataTemplate>

			<DataTemplate x:Key="thumbnailTemplate" DataType="{x:Type local:Transformable}">
				<Grid Margin="0,0,10,10">
					<Grid>
						<Grid Margin="5,5,0,0">
							<Rectangle Fill="White"/>
							<local:TransformedImage Drawing="{Binding Drawing}"/>
							<Grid.Effect>
								<DropShadowEffect Opacity="0.5" />
							</Grid.Effect>
						</Grid>
						<CheckBox HorizontalAlignment="Left" VerticalAlignment="Top" IsChecked="{Binding Selected, Mode=TwoWay}"
								  Width="16" Height="16" Style="{StaticResource thumbnailCheckbox}" />
						<!--<local:Loading Width="50" Height="50" Opacity="0.5" />-->
					</Grid>
				</Grid>
			</DataTemplate>

		</Grid.Resources>

		<Grid.RowDefinitions>
			<RowDefinition Height="Auto"/>
			<RowDefinition/>
		</Grid.RowDefinitions>

		<Menu Grid.Row="0">
			<MenuItem Header="_File">
				<MenuItem Command="ApplicationCommands.Open"/>
				<Separator/>
				<MenuItem Command="ApplicationCommands.SaveAs" IsEnabled="{Binding ElementName=photoPrint, Path=Images.CurrentItem.Selected}"/>
				<Separator/>
				<MenuItem Command="ApplicationCommands.Print" IsEnabled="{Binding ElementName=photoPrint, Path=Images.CurrentItem.Selected}"/>
				<Separator/>
				<MenuItem Command="ApplicationCommands.Close"/>
			</MenuItem>
			<MenuItem Header="_Help">
				<MenuItem Command="local:MainWindow.About" Header="_About"/>
			</MenuItem>
		</Menu>

		<Grid Grid.Row="1" DataContext="{Binding ElementName=photoPrint, Path=Images}">

			<Grid.ColumnDefinitions>
				<ColumnDefinition Width="200"/>
				<ColumnDefinition Width="5"/>
				<ColumnDefinition/>
			</Grid.ColumnDefinitions>

			<Grid Grid.Column="0">

				<Grid.Resources>

					<DataTemplate DataType="{x:Type local:Transformable}">

						<Border Background="White" Margin="5,5,5,10" CornerRadius="10">

							<Border.Effect>
								<DropShadowEffect Opacity="0.5" />
							</Border.Effect>

							<Border Background="#E0E0E0" Padding="10" CornerRadius="10">

								<DockPanel>

									<Expander Header="Print and export" DockPanel.Dock="Bottom">
										<StackPanel>
											<Button Name="printDirect" Click="printDirect_Click" Margin="0,5,0,0" IsEnabled="{Binding Selected}">Print...</Button>
											<Button Name="save" Click="save_Click" Margin="0,5,0,0" IsEnabled="{Binding Selected}">Save...</Button>
										</StackPanel>
									</Expander>

									<Expander Header="Transformations" IsExpanded="True">

										<DockPanel>

											<StackPanel DockPanel.Dock="Bottom">
												<Button Margin="0,5,0,0" Name="rotate" Click="rotate_Click">Rotate</Button>
												<Button Margin="0,5,0,0" Name="crop" Click="crop_Click">Crop</Button>
												<Button Margin="0,5,0,0" Name="straighten" Click="straighten_Click">Straighten</Button>
											</StackPanel>

											<ListBox Name="transformSelect" HorizontalContentAlignment="Stretch"
											 ItemsSource="{Binding Path=Transformations}"
											 IsSynchronizedWithCurrentItem="True"
											 BorderThickness="0"
											 ScrollViewer.HorizontalScrollBarVisibility="Disabled">

												<ListBox.Resources>

													<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#C0C0FF"/>
													<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/>
													<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#C0C0FF"/>

												</ListBox.Resources>

											</ListBox>

										</DockPanel>

									</Expander>

								</DockPanel>

							</Border>

						</Border>

					</DataTemplate>

				</Grid.Resources>

				<ContentControl Content="{Binding CurrentItem}"/>

			</Grid>

			<GridSplitter Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="AliceBlue"/>

			<Grid Grid.Column="2">

				<Grid.RowDefinitions>
					<RowDefinition/>
					<RowDefinition Height="5"/>
					<RowDefinition Height="100"/>
				</Grid.RowDefinitions>

				<Grid Grid.Row="0">

					<Grid HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,5,10,5">

						<Grid>
							<Rectangle Fill="White">
								<Rectangle.Effect>
									<DropShadowEffect Opacity="0.5" />
								</Rectangle.Effect>
							</Rectangle>
							<local:Preview Source="{Binding Path=CurrentItem.Transformations.CurrentItem}"/>
							<!--<local:Loading Width="100" Height="100" Opacity="0.5" />-->
						</Grid>

					</Grid>

				</Grid>

				<GridSplitter Grid.Row="1" HorizontalAlignment="Stretch" Background="AliceBlue"/>

				<Grid Grid.Row="2">

					<ScrollViewer CanContentScroll="False" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled" BorderThickness="0">

						<ListBox Name="imageSelect" VerticalContentAlignment="Top" VerticalAlignment="Stretch" Margin="0,0,5,0"
								 ScrollViewer.VerticalScrollBarVisibility="Disabled" BorderThickness="0"
								 ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" ItemTemplate="{StaticResource thumbnailTemplate}">
							<ListBox.ItemsPanel>
								<ItemsPanelTemplate>
									<StackPanel Orientation="Horizontal" />
								</ItemsPanelTemplate>
							</ListBox.ItemsPanel>
							<ListBox.Resources>
								<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#C0C0FF"/>
								<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/>
								<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#C0C0FF"/>
							</ListBox.Resources>
						</ListBox>

					</ScrollViewer>

				</Grid>

			</Grid>

		</Grid>

	</Grid>

</Window>